package com.example.gamestore.control;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.gamestore.Bean.Detail;
import com.example.gamestore.Bean.Game;
import com.example.gamestore.aop.AdminLogAnnotation;
import com.example.gamestore.mapper.DetailMapper;
import com.example.gamestore.mapper.GameMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class AdminGameControl {
    @Autowired
    private GameMapper gameMapper;
    @Autowired
    private DetailMapper detailMapper;
    @GetMapping("/admin/addgames")
    public String addgames(){
        return "Admin/addgame";
    }
    @PostMapping("/admin/add")
    @AdminLogAnnotation(module = "游戏模块",operater = "添加游戏")
    public String add(Game game,String detail){
        String gamename = game.getGamename();
        game.setViewcount(0);
        game.setImage("assets/images/game/game13.jpg");
        System.out.println(game.getId());
        gameMapper.insert(game);
        detailMapper.insert(new Detail
                (null,gamename,gamename,"1",detail,"游戏背景",
                        "1","游戏团队","1","首次发布","1","1",
                        "兼容于","1","价格和下载","点击下载",game.getId()));
        System.out.println(game.getId());
        return "redirect:/admin/games";
    }
    @PostMapping("/admin/checkname")
    @ResponseBody
    public String checkname(String gamename){
        LambdaQueryWrapper<Game> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Game::getGamename,gamename).last("limit 1");
        Game game = gameMapper.selectOne(wrapper);
        if(game!=null){
            return "该游戏已经存在";
        }else {
            return "此游戏可以使用";
        }

    }
}
